<?php
namespace Modules\ApiAdmin\Http\Controllers;
use Illuminate\Http\Request;
use Modules\Admin\Http\Controllers\CommonController;
use Illuminate\Support\Facades\Cache;
class ApisController  extends CommonController
{
	/**
	 * @name  手机号查询页面   https://www.juhe.cn/
	 */
    public function phone()
    {
		$info = ['name'=>'手机号码归属地查询','keys'=>'phone','url'=>route('api_admin.apis.phone_store')];
		return view('apiadmin::apis.index',compact('info'));
    }
	/**
	 * @name  手机号查询数据处理
	 */
    public function phone_store(Request $request)
    {
		$url = "http://apis.juhe.cn/mobile/get";
		$params = [
			'phone' => $request->get('phone'),
			'key'=>config('apiadmin.juhe_apis_phone_key')
		];
		$content = juheCurl($url,$params);
		return $this->request_return(json_decode($content, true));
    }
	/**
	 * @name  IP查询页面
	 */
    public function ip()
    {
		$info = ['name'=>'IP归属地查询','keys'=>'ip','url'=>route('api_admin.apis.ip_store')];
		return view('apiadmin::apis.index',compact('info'));
    }
	/**
	 * @name  IP查询数据处理
	 */
    public function ip_store(Request $request)
    {
		$url = "http://apis.juhe.cn/ip/ipNew";
		$params = [
			'ip' => $request->get('ip'),
			'key'=>config('apiadmin.juhe_apis_ip_key')
		];
		$content = juheCurl($url,$params);
		return $this->request_return(json_decode($content, true));
    }
	/**
	 * @name  身份证查询页面
	 */
    public function idcard()
    {
		$info = ['name'=>'身份证查询','keys'=>'idcard','url'=>route('api_admin.apis.idcard_store')];
		return view('apiadmin::apis.index',compact('info'));
    }
	/**
	 * @name  身份证查询数据处理
	 */
    public function idcard_store(Request $request)
    {
		$url = "http://apis.juhe.cn/idcard/index";
		$params = [
			'cardno' => $request->get('cardno'),
			'key'=>config('apiadmin.juhe_apis_idcard_key')
		];
		$content = juheCurl($url,$params);
		return $this->request_return(json_decode($content, true));
    }
	/**
	 * @name  QQ测吉凶页面
	 */
    public function qq()
    {
		$info = ['name'=>'QQ测吉凶','keys'=>'qq','url'=>route('api_admin.apis.qq_store')];
		return view('apiadmin::apis.index',compact('info'));
    }
	/**
	 * @name  QQ测吉凶数据处理
	 */
    public function qq_store(Request $request)
    {
		$url = "http://japi.juhe.cn/qqevaluate/qq";
		$params = [
			'qq' => $request->get('qq'),
			'key'=>config('apiadmin.juhe_apis_qq_key')
		];
		$content = juheCurl($url,$params);
		return $this->request_return(json_decode($content, true),'qq');
    }
	/**
	 * @name  重仓股基金页面
	 */
    public function zcgjj(Request $request)
    {
		$status = $request->get('status');
		if($status){
			$this->zcgjj_data_store();
		}
		return view('apiadmin::apis.zcgjj');
    }
	/**
	 * @name  重仓股基金数据处理
	 */
    public function zcgjj_store(Request $request)
    {
		$params = $request->all();
		$zcgjj_data = Cache::get('api_zcgjj_data');
		if(!$zcgjj_data){
			$zcgjj_data = $this->zcgjj_data_store();
		}
		$arr = [];
		if(isset($params['name']) && $params['name'] != ''){
			foreach($zcgjj_data as $k=>$v){
				if(strstr($v['name'],$params['name']) || strstr($v['code'],$params['name'])){
					$arr[] = $v;
				}
			}
		}else{
			$arr = $zcgjj_data;
		}
		return response()->json(['code' => 0, 'msg'=> config('admin.limit_msg'),'data' =>$arr]);
    }
	/**
	 * @name  获取重仓股基金数据
	 */
    private function zcgjj_data_store()
    {
		$url = "http://web.juhe.cn:8080/fund/zcgjj/";
		$params = [
			'key'=>config('apiadmin.juhe_apis_zcgjj_key')
		];
		$content = juheCurl($url,$params);
		$result = json_decode($content,true)['result'];
		Cache::put('api_zcgjj_data',$result[0],3600);
		return $result[0];
    }
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * @name  暂停基金页面
	 */
    public function purch(Request $request)
    {
		$status = $request->get('status');
		if($status){
			$this->purch_data_store();
		}
		return view('apiadmin::apis.purch');
    }
	/**
	 * @name  暂停基金数据处理
	 */
    public function purch_store(Request $request)
    {
		$params = $request->all();
		$purch_data = Cache::get('api_purch_data');
		if(!$purch_data){
			$purch_data = $this->purch_data_store();
		}
		$arr = [];
		if(isset($params['name']) && $params['name'] != ''){
			foreach($purch_data as $k=>$v){
				if(strstr($v['name'],$params['name']) || strstr($v['code'],$params['name'])){
					$arr[] = $v;
				}
			}
		}else{
			$arr = $purch_data;
		}
		return response()->json(['code' => 0, 'msg'=> config('admin.limit_msg'),'data' =>$arr]);
    }
	/**
	 * @name  获取暂停基金数据
	 */
    private function purch_data_store($level = 'purch')
    {
		$url = "http://web.juhe.cn:8080/fund/suspend/" . $level;
		$params = [
			'key'=>config('apiadmin.juhe_apis_purch_key')
		];
		$content = juheCurl($url,$params);
		$result = json_decode($content,true)['result'];
		Cache::put('api_purch_data',$result[0],3600);
		return $result[0];
    }
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * @name  返回值输出
	 */
	private function request_return($result,$re = ''){
		if($result){
			if($re == 'qq'){
				$msg = $result['result']['data'];
			}else{
				$msg = $result['result'];
			}
			return response()->json([
				'status'=>1,
				'error' => '',
				'msg' => $msg
			],200);
		}else{
			return response()->json([
				'status'=>0,
				'error' => config('apiadmin.api_request_error'),
				'msg' => ''
			],201);
		}
	}
}
